package servlet;

import dao.ProductTypeDao;
import bean.Result;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RemoveProductTypeMaxServlet", value = "/RemoveProductTypeMaxServlet.s")
public class RemoveProductTypeMaxServlet extends HttpServlet {

    ProductTypeDao dao = new ProductTypeDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String id = request.getParameter("id");
        final String flag = request.getParameter("flag");
        final String status = request.getParameter("status");
        Result result = null;

        if ("物理删除".equals(flag)){
            int res = dao.removeProductType(id);
            System.out.println(res);
            if (res ==1){
                result = new Result(1,"删除成功!",null);
            }
        } else if ("逻辑删除".equals(flag)){
            dao.removeStatusProductType(id,status);
            if ("0".equals(status)){
                result = new Result(1,"商品禁用",null);
            }else if ("1".equals(status)){
                result = new Result(0,"商品恢复使用",null);
            }
        }
        final String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
